package com.littlebee.storysuper.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.littlebee.storysuper.R;
import com.littlebee.storysuper.entry.BabyStoryPlanEntry;
import com.littlebee.storysuper.entry.StoryInfoEntry;
import com.littlebee.storysuper.entry.User;
import com.littlebee.storysuper.utils.DateUtils;
import com.littlebee.storysuper.utils.GsonUtils;
import com.littlebee.storysuper.utils.NetWorkUtils;
import com.littlebee.storysuper.utils.Utils;
import com.littlebee.storysuper.widget.RoundAngleImageView;
import com.zss.library.activity.BaseActivity;
import com.zss.library.adapter.CommonAdapter;
import com.zss.library.adapter.ViewHolder;
import com.zss.library.toolbar.CommonToolbar;
import com.zss.library.utils.LogUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 今日故事列表
 */
public class TodayStoryInfoActivity extends BaseActivity implements View.OnClickListener {

    private GridView mListView;
    CommonAdapter<StoryInfoEntry> mAdapter;
    List<StoryInfoEntry> babyStoryPlanEntryNames = new ArrayList<StoryInfoEntry>();
    private String storyseriesid;
    private String title;
    @Override
    public int getLayoutResId() {
        return R.layout.activity_today_story_info;
    }

    @Override
    public void initView() {
        super.initView();
        mListView = (GridView) findViewById(R.id.listview);
        title=getIntent().getStringExtra("title");
        storyseriesid=getIntent().getStringExtra("id");

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        loadData();
        mAdapter = new CommonAdapter<StoryInfoEntry>(getActivity(), R.layout.layout_item_story) {
            @Override
            protected void convert(ViewHolder viewHolder, final StoryInfoEntry workerListEntry, int i) {
                TextView name = viewHolder.findViewById(R.id.tv);
                TextView name0 = viewHolder.findViewById(R.id.tv0);

                TextView name1 = viewHolder.findViewById(R.id.tv1);

                RoundAngleImageView iv = viewHolder.findViewById(R.id.photo);
                TextView lt=viewHolder.findViewById(R.id.tv_lt);
                name.setText(workerListEntry.getStoryname());
                name0.setText(workerListEntry.getStorydescription());
                lt.setText(workerListEntry.getStoryhotpoint());
                name1.setText(DateUtils.getTimeString(Long.parseLong(workerListEntry.getStoryduration())*1000));

                //name1.setVisibility(View.GONE);
                // TODO: 2017/8/15
                Glide.with(TodayStoryInfoActivity.this).load(workerListEntry.getStoryminiphotourl()).error(R.mipmap.logo_img).into(iv);


            }
        };
        mListView.setAdapter(mAdapter);
        mAdapter.addAll(babyStoryPlanEntryNames);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(TodayStoryInfoActivity.this, StoryInfoActivity.class);
                intent.putExtra("isInfo", false);
                StoryInfoEntry bean=babyStoryPlanEntryNames.get(position);
                BabyStoryPlanEntry entry=new BabyStoryPlanEntry(bean.getStoryid(),bean.getStoryname(),bean.getStoryminiphotourl(),bean.getStoryduration(),bean.getStoryhotpoint());
                intent.putExtra("info", entry);
                intent.putExtra("isPlan", true);
                startActivity(intent);
            }
        });

    }

    @Override
    public void setTopBar() {
        super.setTopBar();
        setStatusBarColor(R.color.module_video_bg);
        CommonToolbar toolbar = getToolbar();
        toolbar.setBgRes(R.color.module_video_bg);
        toolbar.setTitle(title);

    }

    /*
   * 今日推荐故事
   *
   * */
    public void loadData() {
        Map<String, String> hashMap = new HashMap<>();
        User user = Utils.getUserEntry();
        hashMap.put("phoneId", user.getMobile());
        hashMap.put("storyseriesid", storyseriesid);

        NetWorkUtils.post(getActivity(), "getstoryseriesinfo", hashMap, true, new NetWorkUtils.IListener() {

            @Override
            public void onSuccess(String result, JSONObject resObj) {
                String res = resObj.optString("response")/**.optString("storypages")*/;
                babyStoryPlanEntryNames = GsonUtils.getListFromJSON(res, StoryInfoEntry.class);
                mAdapter.replaceAll(babyStoryPlanEntryNames);
            }

            @Override
            public void onError(String result, String code, String msg) {
                LogUtils.i("XHX", "XHXERROR:" + result);


            }
        });
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
